import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import log from '../utils/log';

@Injectable()
export class RequestInterceptor implements NestInterceptor {
	intercept(context: any, next: CallHandler): Observable<any> {
		var req = context.switchToHttp().getRequest();
		var logger=log.getLogger();
		const now = Date.now();
		return next
		.handle()
		.pipe(
			tap(() => {
				var logtxt=`${req.method} - ${req.url} - ${Date.now() - now}ms \n headers:${JSON.stringify(req.headers,null,2)}\nparams:${JSON.stringify(req.params,null,2)}\nbody:${JSON.stringify(req.body,null,2)}`
				logger.info(logtxt)
			}),
		);
	}
}
